package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.TableQueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * ClassName DepartmentServiceImpl
 * Create by Mike
 * Date 2021/8/1 16:46
 */
@Service
@Transactional
@Setter
@Slf4j
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void save(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public PageInfo<Department> query(TableQueryObject qo) {

        //使用分页插件,传入当前页,每页显示数量
        PageHelper.offsetPage(qo.getOffset(),qo.getLimit());//设置分页信息

        List<Department> departments = departmentMapper.selectForList(qo);

        return new PageInfo(departments);
    }

    @Override
    public Workbook export() {
        List<Department> departments = listAll();
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("部门");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("名称");
        row.createCell(2).setCellValue("简称");
        Department department = null;
        for (int i = 0; i < departments.size(); i++) {
            row = sheet.createRow(i+1);
            department = departments.get(i);
            row.createCell(0).setCellValue(department.getId());
            row.createCell(1).setCellValue(department.getName());
            row.createCell(2).setCellValue(department.getSn());
        }
        return workbook;
    }

    @Override
    public int importExcel(Workbook workbook) {
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        //遍历所有的数据行
        int count = 0;
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String sn = row.getCell(1).getStringCellValue();
            Department department = new Department();
            department.setName(name);
            department.setSn(sn);
            int insert = departmentMapper.insert(department);
            count += insert;
        }
        return count;
    }
}
